# -*- coding: utf-8 -*-

import scrapy

from tutorial.items import TutorialItem


class HupuSpider(scrapy.spiders.Spider):
    name = "hp"
    allowed_domains = ["hupu.com"]
    start_urls = [
        "https://nba.hupu.com/"
    ]

    def parse(self, response):
        # self.log('A response from %s just arrived!' % response.url)
        # for sel in response.xpath('//dl[@class="gray-list main-stem max250"]/dd'):
        #     item = TutorialItem()
        #     item['title'] = sel.xpath('a/text()').extract()
        #     item['link'] = sel.xpath('a/@href').extract()
        #     yield item

        for sel in response.xpath('//dl[@class="gray-list main-stem max250"]/dd'):
            url = sel.xpath('a/@href').extract()[0]
            yield scrapy.Request(url, callback=self.parse_detail)

    @staticmethod
    def parse_detail(response):
        title = response.xpath('//title/text()').extract()[0]
        print('title= ', title)
        content = response.xpath('//div[@class="quote-content"]/text()').extract()
        print('content= ', content)
        for sel in response.xpath('//div[@class="quote-content"]/div'):
            item = TutorialItem()
            item['link'] = sel.xpath('img').extract()
            yield item



